package com.yanjue.controller;

import com.yanjue.common.entity.BookChapter;
import com.yanjue.common.redisKey.RedisBookKey;
import com.yanjue.common.redisKey.RedisExpire;
import com.yanjue.common.redisKey.RedisService;
import com.yanjue.common.util.Result;
import com.yanjue.common.util.ResultUtil;
import com.yanjue.mapper.BookChapterMapper;
import com.yanjue.service.BookChapterService;
import com.yanjue.vo.BookChapterReadVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 图书章节接口
 */
@Api(description = "章节查询接口")
@RestController
@RequestMapping("book/chapter")
public class BookChapterController {

    @Autowired
    private BookChapterService bookChapterService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private BookChapterMapper bookChapterMapper;

    @ApiOperation(value = "查询图书章节基本信息" , httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "chapterId", value = "章节ID", dataType = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = BookChapter.class)})
    @RequestMapping("/getChapter")
    public Result getChapter(String bookId, Integer chapterId){
        //1.查redis
        String key = RedisBookKey.getBookChapterKey(bookId);
        String field = chapterId.toString();
        BookChapter bookChapter = redisService.getHashVal(key, field, BookChapter.class);
        if (null == bookChapter){
            //2.查db
           bookChapter = bookChapterMapper.selectById(chapterId);
           if (null != bookChapter){
               //写入redis
               redisService.setHashValExpire(key,field,bookChapter, RedisExpire.MONTH);
           }
        }
        return ResultUtil.success(bookChapter);
    }





    @ApiOperation(value = "查询图书章节列表信息" , httpMethod = "GET")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = BookChapter.class)})
    @RequestMapping("/getChapterList")
    public Result getBookChapterList(String bookId) {
        return this.bookChapterService.getBookChapterListByBookId(bookId);
    }






    @ApiOperation(value = "阅读内容" , httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "chapterId", value = "章节ID", dataType = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = BookChapterReadVO.class)})
    @RequestMapping("/readChapter")
    public Result<BookChapterReadVO> readChapter(String bookId, Integer chapterId){
        return this.bookChapterService.readChapter(bookId, chapterId);
    }
}
